// pages/pre-assign/scan-device/scan-device.js
var app = getApp()
var verify = require('../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assignDevData: {},
    taskId: '',
    hidden: true,
    dhidden: true,
    nocancel: true,
    modalMes: '',
    scanBtnhidden: true,
    finishBtnhidden: true,
    modalDMes: '',
    toastHidden: true,
    noassidev: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //console.log(options.taskId)
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      taskId: options.taskId
    })
    this.getTaskDetail();

  },

  getTaskDetail: function() {
    var thm = this;
    wx.request({
      url: app.globalData.appbaseUrl + 'preAssignTask/detail/' + thm.data.taskId,
      method: 'get',
      header: {
        'AUTHORIZATION': app.globalData.token
      },
      success: function(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          if (res.data.data.totalAmount == res.data.data.doneAmount) {
            thm.setData({
              finishBtnhidden: false,
              scanBtnhidden: true
            })
          } else {
            thm.setData({
              finishBtnhidden: true,
              scanBtnhidden: false
            })
          }
          thm.setData({
            assignDevData: res.data.data,
          });
          if (res.data.data.assignDetailVos.length == 0) {
            thm.setData({
              noassidev: false
            })
          } else {
            thm.setData({
              noassidev: true
            })
          }
        } else {
          that.setData({
            dhidden: false,
            modalDMes: res.data.message
          })
        }
      }
    })
  },
  scanDev: function() {
    var that = this;
    wx.scanCode({
      success(res) {
        var deviceId = '';
        if (res.result.indexOf('http:') >= 0) {
          deviceId = res.result.split('?qrId=')[1];
        } else {
          deviceId = res.result.split('deviceid=')[1];
        }
        that.addDev(deviceId);
      }
    })
  },
  addDev: function(qrCodeId) {
    var that = this;
    wx.request({
      url: app.globalData.appbaseUrl + 'preAssignTask/addTo/' + that.data.taskId,
      dataType: 'json',
      contentType: "application/json",
      data: JSON.stringify({
        qrCodeId: qrCodeId
      }),
      method: 'put',
      header: {
        'AUTHORIZATION': app.globalData.token
      },
      success: function(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          that.getTaskDetail();
          wx.showToast({
            title: '分配成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          that.setData({
            hidden: false,
            modalMes: res.data.message,
            //toastHidden:true  
          })
        }
      }
    })
  },
  confirm: function() {
    var that = this;
    that.setData({
      hidden: true,
      modalMes: ''
    })
  },
  dconfirm: function() {
    var that = this;
    that.setData({
      dhidden: true,
      modalDMes: ''
    })
  },
  finish: function() {
    wx.navigateBack({
      // url: 'region/region',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})